new Vue({
    el:'#main',
    data:()=>({
        page:1,
        pageSize:10,
        searchText:'',
        list:[],
        requestUrl:'',
        futureInfo:[],
        underwrite:2,               //  是否只显示包销订单
        salername:[],
        saler_id:0,
        saleStatusStr:'4,5,6,7,8,9',
        typeNum:{
            allNum:0,
            newNum:0,
            makeIdNum:0,
            finishNum:0,
            receiveNum:0,
            delNum:0,
            deliveryNum:0,
            needPayNum:0,
            futureNum:0,
        },
        isdisabled:false,
        auth_ids:$('#adminauth_ids').text(),
        saleRemarkData:[],
        remarkPrivate:false,
        addRemarkText:'',
        sale_id:0,
        myDate:new Date,
        receive_at:0,
        isshow_search:false
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(){
            $('.loding').show()
            var data={
                searchText:this.searchText,
                page:this.page,
                limit:this.pageSize,
                underwrite:this.underwrite,
                saler_id:this.saler_id,
            }
            $.ajax({
                url:this.requestUrl,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    this.list=data.data.data
                    this.typeNum = data.data.typeNum
                    this.page = data.data.page
                    this.salername = data.data.salername
                    if(data.data.pageSum){
                        this.Resetnav(data.data.page-0,data.data.pageSum)
                    }
                    $('.loding').hide()
                }
            })
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        // 搜索
        searchText_(){
            this.page = 1
            this.isshow_search = true
            this.searchText = $('#searchText').val()
            this.getdata()
        },
        changeSaler(){
            this.page = 1
            this.isshow_search = true
            this.getdata()
        },
        tabJump(text){
            this.requestUrl = '/admin/sale/list?request=requestAjax&whereText='+text
            this.getdata()
        },
        // 是否包销
        underwrite_(){
            if(this.underwrite == 1){
                this.underwrite = 2
            }else{
                this.underwrite = 1
            }
            this.getdata()
        },
        saleRemark(id,type,receive_at=0){
            if(type == 'get'){
                this.receive_at = receive_at
                this.point(1,'#sale_remarkModal .alert','hide',1);  
                this.sale_id = id
                var data = {
                    id:id,
                    requestType:'get'
                }    
            }else{
                var data = {
                    sale_id:this.sale_id,
                    requestType:'save',
                    data:this.saleRemarkData
                }
            }
            $.ajax({
                url:"/admin/sale/saleRemark",
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    if(type == 'get') this.saleRemarkData = data
                    if(type == 'save'){
                        if(data.status == 1){
                           $(sale_remarkModal).modal('toggle') 
                            this.point(data.msg,'#mainList .alert','show',data.status);    
                        } else{
                            this.point(data.msg,'#sale_remarkModal .alert','show',data.status);  
                        }
                    }
                    
                }
            })
        },
        remarkPrivate_(){
            this.remarkPrivate = !this.remarkPrivate
        },
        addRemark(){
            if(this.addRemarkText){

                if( $('#addRemarkTextType').val() == -1 ){
                    if(this.saleRemarkData == undefined) this.saleRemarkData = []
                    this.saleRemarkData.push({
                        id:0,
                        sale_id:this.sale_id,
                        sale_detail_id:0,
                        admin_id:0,
                        add:0,
                        private:this.remarkPrivate ? 1 : 0,
                        show2make:0,
                        remark:this.addRemarkText,
                        update_name:$('#admin_name').val(),
                        update_time:this.myDate.getFullYear() + '-' + (this.myDate.getMonth()+1) + '-' + this.myDate.getDate() + ' ' + this.myDate.getHours() + '-' + this.myDate.getMinutes() + '-' + this.myDate.getSeconds()
                    })    
                }else{
                    this.saleRemarkData[$('#addRemarkTextType').val()].private = this.remarkPrivate
                    this.saleRemarkData[$('#addRemarkTextType').val()].remark = this.addRemarkText
                    this.saleRemarkData[$('#addRemarkTextType').val()].update_name = $('#admin_name').val()
                    this.saleRemarkData[$('#addRemarkTextType').val()].update_time = this.myDate.getFullYear() + '-' + (this.myDate.getMonth()+1) + '-' + this.myDate.getDate() + ' ' + this.myDate.getHours() + '-' + this.myDate.getMinutes() + '-' + this.myDate.getSeconds()
                    $('#addRemarkTextType').val(-1)
                }
                this.addRemarkText = '';
            }else{
                this.point('请填写备注内容','#sale_remarkModal .alert','show',2);
            }
            
        },
        // 备注编辑，删除
        remarkOption(index,type){
            if(type == 'del'){
                this.saleRemarkData.splice(index,1)
            }else{
                if(this.saleRemarkData[index].private == 1){
                    this.remarkPrivate = true
                    $('#remarkPrivate_').prop('checked',true)
                } else{
                    this.remarkPrivate = false
                    $('#remarkPrivate_').prop('checked',false)
                }
                this.addRemarkText = this.saleRemarkData[index].remark
                $('#addRemarkTextType').val(index)
            }
        },
        print(id){
           $.ajax({
                url:"/admin/sale/print?id="+id,
                type:'post',
                success:(data)=>{
                }
            }) 
        },
        dataDel(key,id,mysqlTable){
            this.isdisabled = true
            $.ajax({
                url:"/admin/sale/dataDel?id="+id+'&table='+mysqlTable,
                type:'post',
                success:(data)=>{
                    this.isdisabled = false
                    data = JSON.parse(data)
                    if(data.status == 1){
                        this.list.splice(key,1)
                    }
                    this.point(data.msg,'.alert','show',data.status);
                }
            })
        },
        checkFuture(key){
            this.futureInfo = this.list[key];
        },
        point(msg,classText,type,status=0){
            change_mes_type(classText,status)
            if(type == 'show'){
                $(classText).show()
            }else{
                $(classText).hide()
            }
            $(classText+' p').html('*'+msg+'<br>')
        },
        //关闭搜索内容
        shdow(){
            $('#name').val('')
            this.isshow_search=false
            this.getdata()
        },
        setStorage(){
            var storage=window.localStorage;
            storage.setItem("sale_page", this.page);
            storage.setItem("sale_saler_id", this.saler_id);
            storage.setItem('sale_searchText',$('#searchText').val());
        },
    },
    mounted(){
        if($('#formMenu').text() != 1){
            if(window.localStorage.getItem('sale_page')){  
                this.page=window.localStorage.getItem('sale_page')
                this.saler_id=window.localStorage.getItem('sale_saler_id')
                this.searchText=window.localStorage.getItem('sale_searchText')
            }
        }
        window.localStorage.removeItem('sale_page')
        window.localStorage.removeItem('sale_saler_id')
        window.localStorage.removeItem('sale_searchText')    

        if(this.saler_id != 0 || this.searchText != '') this.isshow_search = true
        
        
        this.requestUrl = "/admin/sale/list?request=requestAjax&whereText=all";
        this.getdata()
    }
})